package com.desmart.renjiao.orgainzation;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.desmart.renjiao.R;
import com.desmart.renjiao.bean.Company;
import com.desmart.renjiao.bean.MemberInfo;
import com.desmart.renjiao.contants.Constants;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;
import com.lemon.library.utils.PinyUtils;
import com.lemon.library.view.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Route(path = ARoutePath.addresslist.DepartmentMemberListActivity)
public class DepartmentMemberListActivity extends CommonActivity<DepartmentMemberM,DepartmentMemberP> implements DepartmentMember.DepartmentMemberV {

    private RecyclerView recyclerview;
    private Company.Department item;
    private List<MemberInfo> mList = new ArrayList<>();
    private MemberItemAdapter mAdapter;
    private SideBar sidebar;
    private LinearLayoutManager layoutManager;

    @Override
    public int bindLayout() {
        return R.layout.activity_department_member_list;
    }

    @Override
    public void initView(Activity activity) {
        sidebar = activity.findViewById(R.id.sild_bar);
        recyclerview = activity.findViewById(R.id.recyclerView);
        layoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(layoutManager);
        mAdapter = new MemberItemAdapter(mList);
        recyclerview.setAdapter(mAdapter);

        item = (Company.Department) getIntent().getSerializableExtra(Constants.Department.Key_DepartMent);
        if (item == null)
            return;
        setTitleText(item.getDepartmentname());
        initSearchView(activity);
        sidebar.setVisibility(View.GONE);
    }

    private void initSearchView(Activity activity) {
        ImageView ivRight = activity.findViewById(R.id.common_iv_right);
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setOnClickListener(this);
    }

    @Override
    public void initData(Context context) {
        mPresenter.getMember(item.getDepartmentid(),item.getType());
    }

    @Override
    public void onMemberList(List<MemberInfo> users) {

        Collections.sort(users, new Comparator<MemberInfo>() {
            @Override
            public int compare(MemberInfo user1, MemberInfo user2) {
                String name1 = user1.getUsername();
                String name2 = user2.getUsername();
                int flag = PinyUtils.getAlpha(name1).compareTo(PinyUtils.getAlpha(name2));
                return flag;
            }
        });

        mList.clear();
        mList.addAll(users);
        initSilderBar();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onNetStart(String tag, String startMsg) {
        super.onNetStart(tag, startMsg);
        showLoading();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.common_iv_right){
            ARouter.getInstance().build(ARoutePath.addresslist.MemberSearchActivity).navigation();
        }
    }

    private void initSilderBar() {
        sidebar.setVisibility(View.VISIBLE);
        List<String> keylist = new ArrayList<>();
        for (MemberInfo item : mList) {
            String alpha = PinyUtils.getAlpha(item.getUsername());
            if (!keylist.contains(alpha))
                keylist.add(alpha);
        }

        String[] keys = keylist.toArray(new String[keylist.size()]);
        sidebar.setChilden(keys);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) sidebar.getLayoutParams();
        lp.height = keys.length * 40 + 100;
        sidebar.setLayoutParams(lp);

        sidebar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String str) {
                for (int i = 0; i < mList.size(); i++) {
                    MemberInfo item = mList.get(i);
                    String alpha = PinyUtils.getAlpha(item.getUsername());
                    if (str.equals(alpha)) {
                        moveToPosition(i);
                        return;
                    }
                }

            }
        });
    }

    private void moveToPosition(int n) {
        //先从RecyclerView的LayoutManager中获取第一项和最后一项的Position
        int firstItem = layoutManager.findFirstVisibleItemPosition();
        int lastItem = layoutManager.findLastVisibleItemPosition();
        //然后区分情况
        if (n <= firstItem ){
            //当要置顶的项在当前显示的第一个项的前面时
            recyclerview.scrollToPosition(n);
        }else if ( n <= lastItem ){
            //当要置顶的项已经在屏幕上显示时
            int top = recyclerview.getChildAt(n - firstItem).getTop();
            recyclerview.scrollBy(0, top);
        }else{
            //当要置顶的项在当前显示的最后一项的后面时
            recyclerview.scrollToPosition(n);
        }

    }



}
